<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title></title>
    <style></style>
  </head>
  <body>
    <div id="test"></div>
    <!-- 引入react核心库 -->
    <script src="../js/react.development.js"></script>
    <!-- 引入react Dom库 用于支持 react操作dom-->
    <script src="../js/react-dom.development.js"></script>
    <!-- 引入babel 用于将 jsx 转为 js -->
    <script src="../js/babel.min.js"></script>

    <!-- 使用babel 转译jsx -->
    <script type="text/babel">
      class A extends React.Component {
        state = {
          hasError: "", //用于标记子组件是否产生错误
        };
        static getDerivedStateFromError(error) {
          // 这个组件的任何子组件出现报错式就会调用，并且携带错误信息
          return { hasError: error };
        }

        componentDidCatch() {
          //可以在这个生命周期里面告诉后台
          console.log("数据出错了");
        }

        render() {
          return (
            <div>
              <div>我是A组件</div>
              {this.state.hasError ? <h2>发生错误</h2> : <B />}
            </div>
          );
        }
      }
      class B extends React.Component {
        state = {
          //   users: [
          //     { id: "001", name: "tom", age: 18 },
          //     { id: "002", name: "jack", age: 19 },
          //     { id: "003", name: "peiqi", age: 20 },
          //   ],
          users: "1",
        };
        render() {
          return (
            <div>
              <div>我是B组件</div>
              {this.state.users.map((userObj) => {
                return (
                  <h4 key={userObj.id}>
                    {" "}
                    {userObj.name}---- {userObj.age}
                  </h4>
                );
              })}
            </div>
          );
        }
      }
      ReactDOM.render(<A />, document.getElementById("test"));
    </script>
  </body>
</html>
